<template>
  <div class="home">
    <div class="search">
      <form action="/">
        <van-search
          v-model="value"
          show-action
          placeholder="请输入搜索关键词"
          @search="onSearch"
          @cancel="onCancel"
        />
      </form>
    </div>

    <div class="nav">
      <van-tabs v-model="active" color="#e4393c" swipeable>
        <van-tab title="推荐">
          <div>
            <div class="swiped">
              <van-swipe :autoplay="3000" width="100%" height="auto">
                <van-swipe-item v-for="(image, index) in swipe" :key="index">
                  <img class="auto-img" v-lazy="image" />
                </van-swipe-item>
              </van-swipe>
            </div>

            <div class="icon1">
              <div class="iconbox" @click="viewpaihangbang()">
                <img
                  class="auto-img"
                  src="https://fdfs.xmcdn.com/group56/M09/3B/BD/wKgLgFyQujLCAVYRAAAuwA7DZMg198.png"
                  alt=""
                />
                <div>必听榜</div>
              </div>

              <div class="iconbox" @click="viewtingdan()">
                <img
                  class="auto-img"
                  src="https://fdfs.xmcdn.com/group57/M0A/3B/D0/wKgLgVyQuqWRZsJtAAAnr8tg4rs307.png"
                  alt=""
                />
                <div>听单</div>
              </div>
            </div>
          </div>

          <!-- 排行榜 -->
          <div class="you">
            <div class="xiang">评书热播榜</div>

            <div
              class="ypai"
              :data-id="item.id"
              v-for="(item, index) in rankxp"
              :key="index"
              @click="viewAudio(item.id)"
            >
              <div class="yindex">{{ index + 1 }}</div>
              <div class="yimg">
                <img
                  class="auto-img"
                  :src="'https://imagev2.xmcdn.com/' + item.cover"
                  alt=""
                />
              </div>
              <div class="ytext">
                <div class="ytitle one-text">
                  {{ item.title }}
                </div>
                <div class="yxiang two-text">
                  {{
                    item.shortIntro === ""
                      ? " 就这么擦肩而过了离开时他是沉默的沉默的人就不会哭么还有什么遗憾么遗憾是来不及告别的"
                      : item.shortIntro
                  }}
                </div>
              </div>
            </div>
          </div>
        </van-tab>

        <van-tab title="有声书">
          <div>
            <div class="swiped">
              <van-swipe :autoplay="3000" width="100%" height="auto">
                <van-swipe-item v-for="(image, index) in swipe1" :key="index">
                  <img class="auto-img" v-lazy="image" />
                </van-swipe-item>
              </van-swipe>
            </div>

            <div class="icon">
              <div
                class="iconbox"
                v-for="(item, index) in tomatoes1"
                :key="index"
                @click="viewtomatoes(item.link, item.name)"
              >
                <img class="auto-img" :src="item.img" alt="" />
                <div>{{ item.name }}</div>
              </div>
            </div>
          </div>

          <!-- 有声书排行榜 -->
          <div class="you">
            <div class="xiang">有声书评书热播榜</div>
            <div
              class="ypai"
              :data-id="item.id"
              v-for="(item, index) in album1"
              :key="index"
              @click="viewAudio(item.id)"
            >
              <div class="yindex">{{ index + 1 }}</div>
              <div class="yimg">
                <img class="auto-img" :src="item.cover" alt="" />
              </div>
              <div class="ytext">
                <div class="ytitle one-text">
                  {{ item.title }}
                </div>
                <div class="yxiang two-text">
                  {{ item.shortIntro }}
                </div>
              </div>
            </div>
          </div>
        </van-tab>

        <van-tab title="儿童">
          <div>
            <div class="swiped">
              <van-swipe :autoplay="3000" width="100%" height="auto">
                <van-swipe-item v-for="(image, index) in swipe2" :key="index">
                  <img class="auto-img" v-lazy="image" />
                </van-swipe-item>
              </van-swipe>
            </div>
            <div class="icon">
              <div
                class="iconbox"
                v-for="(item, index) in tomatoes2"
                :key="index"
                @click="viewtomatoes(item.link, item.name)"
              >
                <img class="auto-img" :src="item.img" alt="" />
                <div>{{ item.name }}</div>
              </div>
            </div>
          </div>

          <!-- 儿童排行榜 -->
          <div class="you">
            <div class="xiang">儿童评书热播榜</div>
            <div
              class="ypai"
              :data-id="item.id"
              v-for="(item, index) in album3"
              :key="index"
              @click="viewAudio(item.id)"
            >
              <div class="yindex">{{ index + 1 }}</div>
              <div class="yimg">
                <img class="auto-img" :src="item.cover" alt="" />
              </div>
              <div class="ytext">
                <div class="ytitle one-text">
                  {{ item.title }}
                </div>
                <div class="yxiang two-text">
                  {{ item.shortIntro }}
                </div>
              </div>
            </div>
          </div>
        </van-tab>

        <van-tab title="音乐">
          <div>
            <div class="swiped">
              <van-swipe :autoplay="3000" width="100%" height="auto">
                <van-swipe-item v-for="(image, index) in swipe3" :key="index">
                  <img class="auto-img" v-lazy="image" />
                </van-swipe-item>
              </van-swipe>
            </div>
            <div class="icon">
              <div
                class="iconbox"
                v-for="(item, index) in tomatoes3"
                :key="index"
                @click="viewtomatoes(item.link, item.name)"
              >
                <img class="auto-img" :src="item.img" alt="" />
                <div>{{ item.name }}</div>
              </div>
            </div>
          </div>

          <!-- 音乐排行榜 -->
          <div class="you">
            <div class="xiang">音乐评书热播榜</div>
            <div
              class="ypai"
              :data-id="item.id"
              v-for="(item, index) in album5"
              :key="index"
              @click="viewAudio(item.id)"
            >
              <div class="yindex">{{ index + 1 }}</div>
              <div class="yimg">
                <img class="auto-img" :src="item.cover" alt="" />
              </div>
              <div class="ytext">
                <div class="ytitle one-text">
                  {{ item.title }}
                </div>
                <div class="yxiang two-text">
                  {{ item.shortIntro }}
                </div>
              </div>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      active: 0,
      tomatoes: [],
      value: "",

      //推荐
      swipe: [],
      rank: [],
      rank1: [],
      rankxp: [],

      //有声书
      swipe1: [],
      ranky: [],
      album: [],
      album1: [],
      tomatoes1: [],

      //儿童
      swipe2: [],
      ranky2: [],
      album2: [],
      album3: [],
      tomatoes2: [],

      //音乐
      swipe3: [],
      ranky3: [],
      album4: [],
      album5: [],
      tomatoes3: [],
    };
  },
  created() {
    this.getSwipe();
    this.getYou(
      "youshengshu",
      this.swipe1,
      this.ranky,
      this.album,
      this.album1,
      this.tomatoes1
    );
    this.getYou(
      "ertong",
      this.swipe2,
      this.ranky2,
      this.album2,
      this.album3,
      this.tomatoes2
    );
    this.getYou(
      "yinyue",
      this.swipe3,
      this.ranky3,
      this.album4,
      this.album5,
      this.tomatoes3
    );
  },
  methods: {
    getSwipe() {
      this.axios({
        method: "get",
        url: "https://apis.netstart.cn/ximalaya/index/queryIndexTabContent",
      })
        .then((result) => {
          this.$toast("加载成功");
          let focusImages = result.data.data.moduleContent.focusImages;
          let moduleRankDatas = result.data.data.moduleContent.moduleRankDatas;

          //首页轮播图
          focusImages.forEach((v) => {
            this.swipe.push(v.adFocusPictureDetail.cover);
          });

          //相声排行榜
          moduleRankDatas.forEach((v) => {
            this.rank.push(v.rankingContentInfoList);
          });
          this.rank.forEach((v) => {
            for (var i = 0; i < v.rankModuleInfoList.length; i++) {
              this.rankxp.push(v.rankModuleInfoList[i].albumInfo);
            }
          });
          this.rankxp.shift();
        })
        .catch((err) => {
          
        });
    },

    getYou(name, swipe, ranky, album, album1, tomatoes) {
      this.axios({
        method: "get",
        url:
          "https://apis.netstart.cn/ximalaya/index/queryIndexCategoryTabContent?moduleKey=" +
          name,
      })
        .then((result) => {
          let focusImages = result.data.data.moduleContent.focusImages;
          let moduleRankDatas = result.data.data.moduleContent.moduleRankDatas;

          let tom = result.data.data.moduleContent.tomatoes;
          tom.forEach((v) => {
            tomatoes.push(v);
          });
          tomatoes.pop();

          //首页轮播图
          focusImages.forEach((v) => {
            swipe.push(v.adFocusPictureDetail.cover);
          });

          // 评书排行榜
          moduleRankDatas.forEach((v) => {
            ranky.push(v);
          });
          ranky.forEach((v) => {
            album.push(v.albumBriefDetailInfos);
          });
          album[0].forEach((v) => {
            album1.push(v.albumInfo);
          });
        })
        .catch((err) => {
          
        });
    },
    //播放
    viewAudio(trackid) {
      this.$router.push({ name: "Audio", params: { trackid } });
    },
    viewpaihangbang() {
      this.$router.push({ name: "Paihangbang" });
    },
    viewtingdan() {
      this.$router.push({ name: "Tingdan" });
    },
    viewtomatoes(link, name1) {
      this.$router.push({ name: "Tomatoes", params: { link, name1 } });
    },
    onSearch(val) {
      if(val===''||val===undefined){
        this.$toast("搜索不能为空");
        return;
      }
      this.$router.push({name:'Search',params: { val }})
    },
    onCancel() {
      this.$toast("取消");
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.van-search {
  padding-bottom: 2px;
}
/deep/.van-search__action{
  background-color: #F7F8FA;
  color: #333;
  border-left: 2px solid #ddd;
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
  box-sizing: border-box;
}

.nav {
  margin-bottom: 50px;
}
.swiped {
  width: 100%;
  margin-top: 10px;
}
.icon1 {
  width: 100%;
  margin-top: 15px;
  padding: 0 80px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  .iconbox {
    width: 50px;
    text-align: center;
  }
}
.icon {
  width: 100%;
  margin-top: 15px;
  padding: 0 40px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  .iconbox {
    width: 50px;
    text-align: center;
  }
}

.you {
  width: 100%;
  margin-bottom: 50px;
  padding: 0 5px;
  box-sizing: border-box;
  margin-bottom: 50px;
  .xiang {
    width: 100%;
    height: 40px;
    font-size: 18px;
    color: #444;
    padding: 10px 0 10px 10px;
    letter-spacing: 2px;
    box-sizing: border-box;
  }
  .ypai {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 5px 10px;
    box-sizing: border-box;
    background-color: #f7f8fa;
    margin-bottom: 10px;
    border-radius: 10px;
    .yindex {
      width: 15%;
      text-align: center;
      line-height: 100px;
      font-size: 20px;
      color: #444;
    }
    .yimg {
      width: 30%;
    }
    .ytext {
      width: 52%;
      .ytitle {
        width: 100%;
        font-size: 18px;
      }
      .yxiang {
        width: 100%;
        font-size: 12px;
        color: #999;
        margin-top: 20px;
      }
    }
  }
}
</style>